package com.estate.struts.actions;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.estate.business.BillingService;
import com.estate.business.UserInfoService;
import com.estate.utils.BillingStateUtil;
import com.estate.utils.ProCatagoryUtil;
import com.estate.vo.impl.BillingVo;
import com.estate.vo.impl.UserInfoVo;

public class CheckAction extends EstateSupport{
	
	private BillingService billingService;
	private UserInfoService userInfoService;
	
	private String multiType;
	private String multiDelete;
	private String delStr;
	private String deleteId;
	private int modifyId;
	private String decision;
	
	private String method;
	
	public String execute() throws Exception {
		System.out.println("in billing");
		String page = "";

		if ("list".equals(method)) {

			List<BillingVo> voList = billingService.findAllBilling();
			voList.removeAll(billingService.findBillByState(1));
			voList.removeAll(billingService.findBillByState(2));
			if (this.getMultiType() != null
					&& this.getMultiType().equals("true")) {
				this.getRequest().setAttribute("multiType", new Boolean(true));
			} else {
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			for(int i=0;i<voList.size();i++)
					voList.get(i).setStateName(BillingStateUtil.getBillingStateString(voList.get(i).getState()));
			
			this.getSession().setAttribute("check", voList);
			page = "list";

		} else if ("delete".equals(method)) {
			if ("true".equals(multiDelete)) {
				System.out.println("!!!!!!!!!" + delStr);
				if (delStr != null && !"".equals(delStr) && !",".equals(delStr)) {
					String[] delArray = delStr.split(",");
					for (int i = 0; i < delArray.length; i++) {
						billingService
								.deleteByPK(Integer.parseInt(delArray[i]));
					}
				}
			} else {
				billingService.deleteByPK(Integer.parseInt(deleteId));
			}
			page = "refresh";
		}   else if("modify".equals(method)){
			BillingVo modifyList=billingService.findBillingByPK(modifyId);
			modifyList.setProCatagoryName(ProCatagoryUtil.getProCatagoryString(modifyList.getProCatagory()));
			this.getSession().setAttribute("modifyList", modifyList);
			this.getSession().setAttribute("billUser", modifyList.getBillUser());
			this.getSession().setAttribute("cashUser", modifyList.getCashUser());
			page="modify";
		} else if("update".equals(method)){
			BillingVo vo=billingService.findBillingByPK(modifyId);
			if("agree".equals(decision)){
				vo.setState(5);
			} else if("disagree".equals(decision)){
				vo.setState(4);
			}
			vo.setCheckUser(this.getSessionUser());
			Date date=new Date(System.currentTimeMillis());
			vo.setCashDate(date);
			billingService.update(vo);
			page="refresh";
		}
		return page;
	}


	public void setBillingService(BillingService billingService) {
		this.billingService = billingService;
	}

	public BillingService getBillingService() {
		return billingService;
	}
	
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}

	public String getMultiType() {
		return multiType;
	}

	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}

	public String getMultiDelete() {
		return multiDelete;
	}

	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}

	public String getDelStr() {
		return delStr;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setModifyId(int modifyId) {
		this.modifyId = modifyId;
	}

	public int getModifyId() {
		return modifyId;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMethod() {
		return method;
	}

	public void setDecision(String decision) {
		this.decision = decision;
	}

	public String getDecision() {
		return decision;
	}

	
}
